package com.lv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.lv.common.dto.UserList;
import com.lv.common.untils.RedisCache;
import com.lv.entity.Role;
import com.lv.entity.User;
import com.lv.entity.UserRole;
import com.lv.mapper.UserMapper;
import com.lv.service.RoleService;
import com.lv.service.UserRoleService;
import com.lv.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 凉薄
 * @since 2022-01-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    RoleService roleService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    RedisCache redisCache;
    @Autowired
    UserRoleService userRoleService;
    @Override
    public User getByName(String username) {
      return   getOne(new QueryWrapper<User>().eq("username",username));
    }

    @Override
    public void deleteUser(Long id) {
        removeById(id);
        userRoleService.remove(new QueryWrapper<UserRole>().eq("user_id",id));
    }

    @Override
    public void updateRole(Long userId, Long roleId) {
        userRoleService.remove(new QueryWrapper<UserRole>().eq("user_id",userId));
        userRoleService.save(new UserRole(userId,roleId));
    }


    @Override
    public IPage<UserList> getAll(Integer current, Integer pageSize, String username) {
        IPage<UserList> page = userMapper.selectJoinPage(new Page<UserList>(current, pageSize), UserList.class, new MPJLambdaWrapper<User>()
                .selectAll(User.class)
                .select(UserRole::getRoleId)
                .selectAs(Role::getName, UserList::getRoleName)
                .leftJoin(UserRole.class, UserRole::getUserId, User::getId)
                .leftJoin(Role.class, Role::getId, UserRole::getRoleId)
                .like(Strings.isNotEmpty(username), User::getUsername, username));
        return page;
    }

    @Override
    public String getUserAuthorities(Long id) {
        String authority;
        String cache =  redisCache.getCacheObject("Authorities:" + id);
        if (cache==null) {
            //角色信息
            Role userRole = roleService.getOne(new QueryWrapper<Role>().inSql("id","select role_id from sys_user_role where user_id="+id));
            authority = "ROLE_"+userRole.getName()+",";
            //权限信息

            if(userRole.getId()==0){
                List<String> allAuth = userMapper.getAllAuth();
                String collect = allAuth.stream().collect(Collectors.joining(","));
                authority=authority+collect;
            }else{
                List<String> userAuthor = userMapper.getUserAuthor(id);
                String collect = userAuthor.stream().collect(Collectors.joining(","));
                authority=authority+collect;
            }
            redisCache.setCacheObject("Authorities:"+id,authority,1, TimeUnit.HOURS);
        }else{
            authority=cache;
        }
        return authority;
    }

    @Override
    public void clearAuthoritiesInfo(Long id) {
        redisCache.deleteObject("Authorities:"+id);
    }


}
